package com.interview02;

/**
 * Copyright (C), 2018-2020
 * FileName: Demo3
 * Author:   kongfanyu
 * Date:     2020/12/16 10:53
 */
public class Demo3 {
    public static void main(String[] args) {
        int srcArray[] = {3, 5, 11, 17, 21, 23, 28, 30, 32, 50, 64, 78, 81, 95, 101};
        int index = binarySearch(srcArray,5);
        System.out.println("下标位置:"+index);
    }

    private static int binarySearch(int[] srcArray, int key) {
        int start = 0;
        int end   = srcArray.length  - 1;
        int mid   ;
        while(start <= end){
            mid   = (end  + start) / 2 ;
            if (srcArray[mid] < key){
                start = mid + 1;
            }else if(srcArray[mid] > key){
                end = mid - 1;
            }else{
                return mid;
            }
        }
        return -1;//标记说明没有查找到结果
    }

}
